package com.gurkensalat.ebooks.ebookmarksync.core;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class HelloAndroidActivity extends Activity
{
	SharedPreferences prefs = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.i(Constants.LOG_TAG, "onCreate - HelloAndroidActivity");
		setContentView(R.layout.main);

		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
		
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		prefs.registerOnSharedPreferenceChangeListener(prefListener);

		final Button buttonAttemptSync = (Button) findViewById(R.id.attempt_sync);
		buttonAttemptSync.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				String autosync_interval = prefs.getString("autosync_interval", "300");

				Intent intent = new Intent(Constants.ACTION_ATTEMPT_SYNC);
				intent.putExtra("autosync_interval", autosync_interval);
				view.getContext().sendBroadcast(intent);

				Log.i(Constants.LOG_TAG, "onClick - Intent sent");
			}
		});

		final Button buttonPreferences = (Button) findViewById(R.id.preferences);
		buttonPreferences.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				Intent myIntent = new Intent(view.getContext(), EditPreferencesActivity.class);
				startActivityForResult(myIntent, 0);
			}
		});
	}

	private SharedPreferences.OnSharedPreferenceChangeListener prefListener = new SharedPreferences.OnSharedPreferenceChangeListener()
	{
		public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key)
		{
			Log.i(Constants.LOG_TAG, "onSharedPreferenceChanged " + key);
			if ("autosync_interval".equals(key))
			{
				Log.i(Constants.LOG_TAG, "  changed key " + key);
			}
		}
	};
}
